<template>
  <div>
    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible">
      <el-form
        ref="form"
        :model="formData"
        label-width="80px"
        :rules="formRules"
  
      >
        <el-form-item label="管理员名称" prop="username">
          <el-input v-model="formData.username"></el-input>
        </el-form-item>
        <el-form-item label="管理员密码" prop="password">
          <el-input v-model="formData.password"></el-input>
        </el-form-item>
        <el-form-item label="管理员头像" prop="avatar">
          <el-upload
            class="avatar-uploader"
            action="http://192.168.200.251:8888/admin/storage/create"
            :show-file-list="false"
            :on-success="avatarSuccess"
            :headers="{ 'Shopping-Admin-Token': token }"
            :auto-upload="true"
          >
            <img
              v-if="formData.avatar"
              :src="formData.avatar"
              style="width: 100px"
            />
            <i
              v-else
              class="el-icon-plus avatar-uploader-icon"
              style="
                width: 100px;
                height: 100px;
                border-radius: 10px;
                border: 2px dashed #ddd;
                line-height: 100px;
              "
            >
            </i>
          </el-upload>
        </el-form-item>
        <el-form-item label="管理员角色" prop="roleIds">
          <el-select v-model="formData.roleIds" multiple placeholder="请选择">
            <el-option
              v-for="item in adminStatus"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="cancel">取消</el-button>
        <el-button @click="confirm">确定</el-button>
      </div>
    </el-dialog>
    <div class="userList-container">
      <el-form :model="searchFormData" inline       class="myForm">
       
        <el-form-item prop="username">
          <el-input
            v-model="searchFormData.username"
            placeholder="请输入管理员名称"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="getData">查找</el-button>
          <el-button @click="openDialog('add', '添加', null)" type="primary"
            >添加</el-button
          >
        </el-form-item>
      </el-form>
    </div>
    <div class="table">
      <el-table
        :data="tableData"
        border
        v-loading="tableloading"
        class="myTab"
      >
        <el-table-column
          align="center"
          label="管理员ID"
          prop="id"
          sortable
        ></el-table-column>
        <el-table-column
          align="center"
          label="管理员名称"
          prop="username"
          width="200"
        ></el-table-column>
        <el-table-column
          align="center"
          label="管理员头像"
          prop="avatar"
          width="200"
        >
        <template slot-scope="scope">
                <img :src="scope.row.avatar" width="80" />    
        </template>
        </el-table-column>
        <el-table-column
          align="center"
          label="管理员角色"
          prop="roleIds"
          width="400"
        >
        <template slot-scope="scope">
          <el-tag
          v-for="item in scope.row['roleIds'] "
              :key="item.value"
          >{{item| tableFilter(adminStatus)}}</el-tag>
        </template>
        </el-table-column>
        <el-table-column label="操作" align='center' width="260">
          <template slot-scope=
          "scope">
            <el-button
              type="primary"
              @click="openDialog('edit', '编辑', scope.row)"
              >编辑</el-button
            >
            <el-button type="danger" @click="deleteRow(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="block">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="this.pageInfo.page"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="this.pageInfo.limit"
        background
        layout="total,sizes, prev, pager, next,jumper"
        :total="this.pageInfo.total"
        class="myStyle"
      >
      </el-pagination>
    </div>
  </div>
</template>
  
<script>
import axios from "axios";
export default {
  name: "adminUser",
  componentTitle: "管理员",
  data() {
    return {
      token: sessionStorage.getItem("Shopping-Admin-Token"),
      dialogVisible: false,
      searchFormData: {
        page: 1,
        limit: 20,
        name: "",
        userId: "",
      },
      tableData: [],
      formRules: {
        username: [
          { required: true, message: "请输入管理员名称", trigger: "change" },
        ],
        password: [
          { required: true, message: "请输入管理员密码", trigger: "change" },
        ],
      },
      formData: {
        roleIds: [

        ]
      },
      pageInfo: {
        page: 1,
        limit: 10,
        total: 0,
      },
      tableloading: false,
      dialogType: "",
      dialogTitle: "",
      adminStatus: [
       
      ],
      
    };
  },
  mounted() {
    this.getData();
    this.getRoleData()
  },
      filters: {
    tableFilter (value, options) {
      let index = options.findIndex(item => {
        return Number(item.value) === Number(value)
      })
      if (index < 0) {
        return '---'
      } else {
        return options[index].label
      }
    }
  },
  methods: {
    avatarSuccess(res, file) {
      //
      this.formData.avatar = res.data.url.replace(
        /localhost:8888/g,
        "192.168.200.251:8888"
      );
      // this.dialogFormData.picUrl = res.data.url.replace(/localhost:8888/g, '192.168.200.251:8888')
    },
    cancel() {
      this.$refs.form.resetFields();
      this.dialogVisible = false;
    },
    addData() {
      axios({
        url: "http://192.168.200.251:8888/admin/admin/create",
        method: "post",
        data: {
          ...this.formData,
        },
        headers: {
          "Content-Type": "application/json; charset=utf-8",
          "Shopping-Admin-Token": sessionStorage.getItem(
            "Shopping-Admin-Token"
          ),
        },
      }).then((res) => {
        console.log('test',123)
        if (res.data.errno === 0) {
          this.cancel();
          this.$notify.success({ title: "添加成功" });

          this.getData();
        } else {
          this.$notify.error({ title: "添加失败" });
        }
      });
    },
    editData() {
      axios({
        url: "http://192.168.200.251:8888/admin/admin/update",
        method: "post",
        data: {
          ...this.formData,
        },
        headers: {
          "Shopping-Admin-Token": sessionStorage.getItem(
            "Shopping-Admin-Token"
          ),
        },
      }).then((res) => {
        if (res.data.errno === 0) {
          this.cancel();
          this.$notify.success({ title: "编辑成功" });
          this.getData();
        } else {
          this.$notify.error({ title: "编辑失败" });
        }
      });
    },

    confirm() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          if (this.dialogType === "add") {
            this.addData();
          } else if (this.dialogType === "edit") {
            this.editData();
          }
        } else {
          return false;
        }
      });
    },
    openDialog(type, title, row) {
      this.dialogVisible = true;
      this.dialogType = type;
      this.dialogTitle = title;
      if (type === "edit") {
        this.$nextTick(() => {
          this.formData = Object.assign({}, row);
        });
      }
    },
    deleteRow(row) {
      this.$confirm("确定删除该条数据吗").then(() => {
        axios({
          url: "http://192.168.200.251:8888/admin/admin/delete",
          method: "post",
          data: {
            id: row.id,
          },
          headers: {
            "Content-Type": "application/json; charset=utf-8",
            "Shopping-Admin-Token": sessionStorage.getItem(
              "Shopping-Admin-Token"
            ),
          },
        }).then((res) => {
          console.log("删除", res);
          this.getData();
        });
      });
    },

    getData() {
      (this.tableloading = true),
        axios({
          url: "http://192.168.200.251:8888/admin/admin/list",
          method: "get",
          params: {
            page: this.pageInfo.page,
            limit: this.pageInfo.limit,
            ...this.searchFormData,
          },
          headers: {
            "Content-Type": "application/json; charset=utf-8",
            "Shopping-Admin-Token": sessionStorage.getItem(
              "Shopping-Admin-Token"
            ),
          },
        }).then((res) => {

          console.log("获取用户列表", res);
          (this.tableData = res.data.data.list),
            (this.pageInfo.total = res.data.data.total);
          this.tableloading = false;
        });
    },
    getRoleData() {
      (this.tableloading = true),
        axios({
          url: "http://192.168.200.251:8888/admin/role/options",
          method: "get",
          headers: {
            "Content-Type": "application/json; charset=utf-8",
            "Shopping-Admin-Token": sessionStorage.getItem(
              "Shopping-Admin-Token"
            ),
          },
        }).then((res) => {
          console.log('178', res);
          this.adminStatus=res.data.data.list

        });
    },
    handleCurrentChange(value) {
      console.log("当前页码", value);
      this.pageInfo.page = value;
      this.getData();
    },
    handleSizeChange(value) {
      console.log("当前每页多少条", value);
      this.pageInfo.limit = value;
      this.getData();
    },
  },
};
</script>
<style lang="scss" scoped>
.userList-container {
  display: flex;
  justify-content: flex-end;
  margin-top: 10px;
}

.myForm{
display: flex;
justify-content: flex-end; 
margin-top: 30px;
margin-right: 22px;
}

.myTab{
  width: 95%;
  text-align: center;
  margin: 0 auto;
}

.myDal{
  .input1{
    width:340px; 
    margin-left: 40px;
  }
}
.myStyle{
  margin-top: 70px;
  margin-left: 80px;
}


</style>








    